package service.Impl;

import Dao.Impl.UserDaoImpl;
import Dao.UserDao;
import pojo.PageBean;
import pojo.User;
import service.UserService;

import java.sql.SQLException;
import java.util.List;

/**
 * @author zhangyu
 * @date 2021/11/18    21:16
 */
public class UserServiceImpl implements UserService {
    /**
     * 获取所有用户
     * @return 返回用户列表
     */
    @Override
    public List<User> findAllUser() {
        try {
            UserDao userDao = new UserDaoImpl();
            List<User> userList = userDao.findAllUser();
            if(userList!=null){
                return  userList ;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /***
     * 更新用户
     * @param user 用户实体
     */

    @Override
    public void updateUser(User user) {
        try {
            UserDao userDao = new UserDaoImpl();
            int count = userDao.update(user);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    /***
     * 更新用户
     * @param uid
     * @return
     */
    @Override
    public User findUserByUid(String uid) {
        try {
            UserDao userDao = new UserDaoImpl() ;
            User user = userDao.selectUserById(uid);
            if(user!=null){
                return  user ;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }


    /***
     * 通过uid删除用户
     * @param uid
     */
    @Override
    public void deleteUser(String uid) {
        try {
            UserDao userDao = new UserDaoImpl();
            int i = userDao.deleteUserByUid(uid);
            System.out.println(i);
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }


    /***
     *分页查询用户数据
     * @param currentPage
     * @param pageSize
     * @return
     */

    @Override
    public PageBean<User> findUserByPage(int currentPage, int pageSize) {
        try {
            UserDao userDao = new UserDaoImpl();
            List<User> list = userDao.selectUserByPage(currentPage,pageSize);
            int totalCount = userDao.getTotalCount() ;
            return new PageBean<>(currentPage,pageSize,totalCount,list);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


}
